$(function(){
	var judgegive;
	/*获取文章正文*/
	ym.ajax({
		url:'/article/getYmArticleByArticleId',
		param:{articleId:ym.getQueryStringByName('articleId')},
		success:function(data){
			console.log(data);
			if(data.res == 1){
				var title = data.obj.articleTitle;
				$('.title').text(title);
				$('h2').text(title);
				$('.add-problem-time').find('span').text(data.obj.strCreateTime);
				$('.num').text(data.obj.readNumber);
				$('.article-tab-desc').children().html(data.obj.articleDatail);
			}else{console.log("数据获取失败！")}
		}
	});
	/*回复条数*/
	ym.ajax({
		url:'/article/getYmAnswerList',
		param:{typeId:ym.getQueryStringByName('articleId')},
		success:function(data){
			if(data.res == 1){
				console.log(data);
				var html = "";
				$('.punlun-count-num').text('('+data.obj.replyCount+')');
				$('.hf').text(data.obj.replyCount);
				$('.dianz').text(data.obj.praiseCount);
				judgegive = data.obj.praiseState;
				if(judgegive == 1){
					$('.like').children('img').attr('src','../img/icon-active-dianzan.png');
				}
				html += '<ul class="backwhite replycomment" style="margin-top: 1rem;">';
				for(var i=0;i<data.obj.ymAnswerVOs.length;i++){
					var time = data.obj.ymAnswerVOs[i].createTimeString;
						time = time.substr(5,0);
					if(data.obj.ymAnswerVOs[i].parentId == 0 || data.obj.ymAnswerVOs[i].parentId == null){
						html += '<li class="am-padding-top-xs am-padding-bottom-xs am-padding-left-sm am-padding-right-sm">';
      					html += '<div class="number xxpl font15 colorb6bcce">';
      					html += '<img src="'+data.obj.ymAnswerVOs[i].headimgurl+'">';
      					html += '<div class="w80 am-margin-left-sm">';
      					html += '<h4 class="color4E4E font16">'+data.obj.ymAnswerVOs[i].userName+'</h4>';
      					html += '<p class="color4E4E am-margin-top-0 font14">'+data.obj.ymAnswerVOs[i].answerDes+'</p>';
      					html += '<span class="font13">'+data.obj.ymAnswerVOs[i].address+'<i class="am-margin-left-xs">'+time+'</i></span>'
      					html += '</div>';
      					html += '<span class="plgr" id="'+data.obj.ymAnswerVOs[i].answerDes+'"><img src="../img/pinglun.png"></span>';
				        html += '</div>';  
				        html += '<p style="display:none">';
				        html += '<span class="usid">'+data.obj.ymAnswerVOs[i].answerUserId+'</span>';
				        html += '<span class="usna">'+data.obj.ymAnswerVOs[i].userName+'</span>';
				        html += '</p>';
				        html += '</li>';
      				}else{
      					html += '<li class="am-padding-top-xs am-padding-bottom-xs am-padding-left-sm am-padding-right-sm">';
      					html += '<div class="number xxpl font15 colorb6bcce">';
      					html += '<img src="'+data.obj.ymAnswerVOs[i].headimgurl+'">';
      					html += '<div class="w80 am-margin-left-sm">';
      					html += '<h4 class="color4E4E font16">'+data.obj.ymAnswerVOs[i].userName+'</h4>';
      					html += '<p class="color4E4E am-margin-top-0 font14"><span>回复<i class="color69e600 am-margin-left-xs">'+data.obj.ymAnswerVOs[i].parentName+'</i>:</span class="font14">'+data.obj.ymAnswerVOs[i].answerDes+'</p>';
      					html += '<span class="font13">'+data.obj.ymAnswerVOs[i].address+'<i class="am-margin-left-xs">'+time+'</i></span>'
      					html += '</div>';
      					html += '<span class="plgr"><img src="../img/pinglun.png"></span>';
				        html += '</div>';   	
				        html += '<p style="display:none">';
				        html += '<span class="usid">'+data.obj.ymAnswerVOs[i].answerUserId+'</span>';
				        html += '<span class="usna">'+data.obj.ymAnswerVOs[i].userName+'</span>';
				        html += '</p>';
				        html += '</li>';
      				}
				}
				html += '</ul>';
				$('.pinlun-tab').append(html);
			}else{console.log("数据获取失败！")}
		}
	});
	/*跳转到全部评论*/
	var id = ym.getQueryStringByName('articleId');
	var praiseType = 1;
   	var praiseTypeId = id;
	$('body').on('click','.pingl',function(){
		window.location.href = "quanbuhuifu.html?articleId="+id;
	})
	/*点赞*/
   	$('body').on('click','#dianzan',function(){
   		var praiseType = 1;
   		var praiseTypeId = id;
   		if(judgegive == 1){
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianz').text();
						if(data.obj=='0'){
							$("#shoucang").modal('open');
					   		var numbers = parseInt(numbers)-1;
					   		$('.dianz').text(numbers);
					   		$('#info-text').html('取消点赞');
					   		$('.like').children('img').attr('src','../img/icon-dianzan.png');
					   		setTimeout(function(){ $("#shoucang").modal('close');},800);
					   		judgegive = 0;
					   		return;
						}
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('取消点赞失败');
						setTimeout(function(){ $("#shoucang").modal('close');},800);
					}
				}
			});
   		}else if(judgegive == 0){
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianz').text();
						if(data.obj=='1'){
							$("#shoucang").modal('open');
					   		var numbers = parseInt(numbers)+1;
					   		$('.dianz').text(numbers);
					   		$('#info-text').html('点赞成功');
					   		$('.like').children('img').attr('src','../img/icon-active-dianzan.png');
					   		setTimeout(function(){ $("#shoucang").modal('close');},500);
					   		judgegive = 1;
					   		return;
						}
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('点赞失败');
						setTimeout(function(){ $("#shoucang").modal('close');},500);
					}
				}
			});
   		}else{
   			ym.ajax({
				url:'/communication/addPraise',
				param:{ 
					praiseTypeId:praiseTypeId,
					praiseType:praiseType
				},
				success:function(data){
					var data=JSON.parse(data);
					if(data.res ==1){
						var numbers = $('.dianz').text();
						if(data.obj=='获得豆豆+5'){
							$("#shoucang").modal('open');
					   		var numbers = parseInt(numbers)+1;
					   		$('.dianz').text(numbers);
					   		$('#info-text').html('点赞成功，豆豆+5');
					   		$('.like').children('img').attr('src','../img/icon-active-dianzan.png');
					   		setTimeout(function(){ $("#shoucang").modal('close');},500);
					   		judgegive = 1;
					   		return;
						}
					}else{
						$("#shoucang").modal('open');
			   			$('#info-text').html('点赞失败');
						setTimeout(function(){ $("#shoucang").modal('close');},500);
					}
				}
			});
   		}
   		
   })
   	function isCollect(){
   		/*判断是否已收藏*/
			ym.ajax({
				url:'/usercollectid/whetherCollection',
				param:{ 
					collectionTypeId :praiseTypeId,
				},
				success:function(data){
					if(data.obj ==1){
						$('.collect-icon').attr('src','../img/dynamic-active-star.png');
					}else{
						$('.collect-icon').attr('src','../img/article-sc.png');
						
					}
				}
			});
   	}
   	isCollect();
   	/*收藏*/
   	var count=0;
   	$('#collect').on('click',function(){
   		count++;
   		if(count>5){
   			$("#shoucang").modal('open');
			$('#info-text').html('收藏成功');
			setTimeout(function(){ $("#shoucang").modal('close');},500);
   		}
   		ym.ajax({
			url:'/usercollectid/collection',
			param:{ 
				collectionType:praiseType,
				collectionTypeId :praiseTypeId,
			},
			success:function(data){
				isCollect();
				if(data.resMsg=='保存成功'){
					$("#shoucang").modal('open');
					$('#info-text').html('收藏成功');
					setTimeout(function(){ $("#shoucang").modal('close');},500);

				}else if(data.resMsg=='删除成功'){
					$("#shoucang").modal('open');
					$('#info-text').html('取消收藏');
					setTimeout(function(){ $("#shoucang").modal('close');},500);
				}
			}
		});
   })
})